package com.sun.java8;

import com.sun.org.apache.xml.internal.utils.SystemIDResolver;
import org.junit.Test;

import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class TestLambda {
    @Test
    public void test01(){
        List<Person> peoples = Arrays.asList(
                new Person(1, "张四", 18),
                new Person(1, "张三", 18),
                new Person(3, "张五", 19),
                new Person(4, "张六", 20)
        );
        System.out.println("排序前 " + peoples);
        Collections.sort(peoples, Comparator.comparing(Person::getAge));
        System.out.println("排序后 " + peoples);

    }
    @Test
    public void test02(){
        op(1L,2L,(x,y)->x+y);
    }
    public void op(Long l1,Long l2,MuFunction1<Long,Long> muFunction1){
        System.out.println(muFunction1.getLog(l1,l2));
    }
    @Test
    public void test03(){
       /*
       函数式接口            参数类型        返回类型        用途
       Consumer<T>           T               void        对类型为T的对象应用操作包含方法 void accept(T t)
       Supplier<T>           无               T            返回类型为T的对象，包含方法 T get();
       Function<T,R>          T               R           对类型T的对象应用操作，并返回结果，结果是R类型的对象，包含方法 R apply(T t)
       Predicate<T>           T               boolean     确定类型为T的对象是否满足某约束，并返回boolean值，包含方法 boolean  test(T t)
        */
        happy(10000,(x)-> System.out.println("消费"+ x + "元"));
    }
    public void happy(double money, Consumer<Double> con){
        con.accept(money);
    }
    @Test
    public void test04(){
        List<Integer> numList = getNumList(10, () -> (int)(Math.random()*100));
        System.out.println(numList);
    }
    public List<Integer> getNumList(Integer num, Supplier<Integer> supplier){
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(supplier.get());
        }
        return list;
    }
    @Test
    public void test05(){
        Map map =new HashMap();
        map.put("id",1);
        map.put("name","张三");
        map.put("age",20);
        Person person = getPersonByMap(map, x -> new Person((int)(x.get("id")),String.valueOf(x.get("name")),(int)(x.get("age"))));
        System.out.println(person);
    }
    public Person getPersonByMap(Map map, Function<Map,Person> function){
        return function.apply(map);
    }
    @Test
    public void test06(){
        List<Person> peoples = Arrays.asList(
                new Person(1, "张四", 18),
                new Person(1, "张三", 18),
                new Person(3, "张五", 19),
                new Person(4, "张六", 21)
        );
        List<Person> personsBy = getPersonsBy(peoples, x -> x.getAge() > 20);
        System.out.println(personsBy);
        
    }

    public List<Person> getPersonsBy(List<Person> persons, Predicate<Person> predicate){
        List<Person> pers = new ArrayList<>();
        for (Person person : persons) {
            if (predicate.test(person)) {
                pers.add(person);
            }
        }
        return pers;
    }
}
